﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cosmos.Compiler.Builder;
using Cosmos.Hardware;
using Cosmos.Kernel;
using Cosmos.Sys.Plugs;
using CosView.GUI;
using Cosmos.Sys.FileSystem.Ext2;
using Cosmos.Hardware.Storage.ATA;
using System.IO;

namespace CosView.BootCode
{
    class BootCode
    {
        public bool isRunning = true;
        public bool reboot = false;
        public BootCode()
        {

            Console.WriteLine("Booted CosView.");

            while (isRunning)
            {
                System.Threading.Thread.Sleep(200);
                Console.Write("$ ");
                string line = Console.ReadLine();
                switch (line.ToLower())
                {
                    case "shutdown":
                        isRunning = false;
                        break;
                    case "restart":
                        reboot = true;
                        isRunning = false;
                        break;
                    case "geramy":
                        Console.WriteLine("Hey, Geramy!!! =P");
                        break;
                    case "matheus":
                        Console.WriteLine("Hey, Matheus!!! =P");
                        break;
                    case "vgatest":
                        VGATest();
                        break;
                    case "uitest":
                        DrawTest();
                        break;
                    /*case "dir":
                        ConsoleColor cc = Console.ForegroundColor;
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("ERROR: This command is currently not implemented.");
                        Console.ForegroundColor = cc;
                        break;*/
                }
            }
            if (reboot) Deboot.Reboot();
            else Deboot.ShutDown();
        }

        static void VGATest()
        {
            VGAScreen.SetMode320x200x8();
            VGAScreen.SetPaletteEntry(0, 255, 0, 0);
            VGAScreen.SetPaletteEntry(1, 0, 255, 0);
            VGAScreen.SetPaletteEntry(2, 0, 0, 255);
            VGAScreen.SetPaletteEntry(3, 255, 255, 255);
            VGAScreen.SetPaletteEntry(4, 0, 0, 0);
            VGAScreen.SetPaletteEntry(5, 63, 63, 63);
            VGAScreen.SetPaletteEntry(6, 127, 127, 127);
            VGAScreen.SetPaletteEntry(7, 191, 191, 191);

            for (uint x = 0; x < 320; x++)
            {
                for (uint y = 0; y < 200; y++)
                {
                    VGAScreen.SetPixel(x, y, (uint)(x * y) % 7);
                }
            }
        }

        static void DrawTest()
        {
            Drawer.Init();
            CosForm cf = new CosForm();
            cf.X = 5;
            cf.Y = 5;
            cf.Text = "UI Test";
            cf.ContentText = "UI Is working!!!";
            cf.Width = 15;
            cf.Height = 5;
            cf.Invalidate();
        }
    }
}
